package com.fzy.writeeExam.tencent;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test01 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();

        for(int i = 0;i<N;i++){
            MyQueue myQueue = new MyQueue();
            int x = sc.nextInt();
            List<Integer> list = new ArrayList<>();
            for(int j = 0;j<x;j++){
                String str = sc.nextLine();
                String[] strings = str.split(" ");
                if(strings.length == 2){
                    myQueue.push(Integer.valueOf(strings[1]));
                }else {
                    switch (strings[0]){
                        case "POP" :
                            list.add(myQueue.pop());
                            break;
                        case "TOP" :
                            list.add(myQueue.top());
                            break;
                        case "CLEAR":
                            myQueue.clear();
                            break;
                        case "SIZE":
                            list.add(myQueue.size());
                            break;
                        default:
                    }
                }
            }
            for(int j = 0;j<list.size();j++){
                System.out.println(list.get(i));
            }
        }
    }
}
class MyQueue{

    private int[] arr;
    private int size;
    private int head;
    private int tail;

    public MyQueue() {
        arr = new int[1000];
        size = 0;
        head = 0;
        tail = 0;
    }

    public void push(int val){
        arr[tail] = val;
        tail = (tail + 1) % 1000;
    }

    public int pop(){
        if(head == tail){
            return -1;
        }
        int val = arr[head];
        arr[head] = 0;
        head++;
        size--;
        return val;
    }

    public int top(){
        if(head == tail){
            return -1;
        }
        return arr[head];
    }

    public int size(){
        return size;
    }

    public void clear(){
        head++;
        tail = head;
    }
}
